var myiscroll=null;
var val;
$(document).ready(function(){
	leftmenu("#leftmenu","#wrap");
	
	var yearMonth="";
//	var url = location.href.split('?')[1];
//	if(url!=null){
//		var arr = url.split('&');
//		for(var i = 0 ; i < arr.length;i++){
//			var temp = arr[i];
//			var tempArr = temp.split('=');
//			var key = tempArr[0];
//			if(key == "yearMonth"){
//				yearMonth =tempArr[1];
//			}
//		}
//	}
	yearMonth = getQueryParam("yearMonth");
	var syear,smonth;
	if(yearMonth==""){
		var today = new Date();
		var month = today.getMonth()+1;
		var year = today.getFullYear();
		if(month<10)
			month = "0"+month;
		yearMonth = year+"/"+month;
		syear = today.getFullYear();
		smonth = month;
	}else{
		syear = yearMonth.substr(0,4);
		smonth = yearMonth.substr(5,2);
	}
	var todayt = syear + ' 年    <span class="m-118" >  '+smonth+'   月 </span>';
	$("#myH2").text('');
	$("#myH2").html(todayt);
	ycquery(yearMonth);
	
	$("#errorBtncancel").on("tap",function(){
		$("#errorMask").hide();
	});
	
	$('#bdate').scroller('destroy').scroller({
			preset: 'date',
			dateFormat: 'yymm',
			dateOrder: 'yymm',
			minDate: new Date(2002, 3, 10, 9, 22),
			maxDate: new Date(2030, 7, 30, 15, 44),
			theme: 'ios',
			mode: 'scroller',
			lang: 'zh',
			display: 'bottom',
			animate: 'slideup'
		});
	$(document).on("tap",function(e){
		if($("#errorMask").css("display")!="none")
			$("#errorMask").hide("slow");
		var li = $(e.target).parents("li");
		if($(li).attr("class")=="css-dispose"||$(li).attr("class")=="css-disposed"||$(li).attr("class")=="css-return"){
		var index = (li.parent().children()).index(li);
			console.log($(li).attr("class"));
			console.log(index);
			var empl_rcd = val[index].empl_rcd;
			var pin_take_num = val[index].pin_take_num;
			var startDate = val[index].startDate;
			var strstartDate = startDate.substr(0,10).replace(/-/g,'/');
			var endDate = val[index].endDate;
			var strendDate="";
			if(endDate!=""){
				strendDate = endDate.substr(0,10).replace(/-/g,'/');
			}
			if(val[index].abs_entry_src==1){
				//errorMask("管理员录入，不能查看详情");
				setPromptForInput("管理员录入，不能查看详情");
			}else{
				//location.href = "qjdwork.html?startDate="+strstartDate+"&endDate="+strendDate+"&pin_take_num="+pin_take_num+"&empl_rcd="+empl_rcd+"&yearMonth="+yearMonth;
				var param = "startDate="+strstartDate+"&endDate="+strendDate+"&pin_take_num="+pin_take_num+"&empl_rcd="+empl_rcd+"&yearMonth="+yearMonth;
				gotoLocation("/mobile/mywork/qjdwork.html",param);
			}
		}
	});
});
function ycquery(yearMonth){
	$("#ullist").empty();
	$("#loadingText").show();
	iscroll();
	$.ajax({
		type: "post",
		url: mpsurl+"/mobile/leave/queryLeaveList.do",
		dataType: "json",
		data:{yearMonth:yearMonth},
		success: function (data) {
			$("#ullist").empty();
			if(data.resultCode=="500600"){
				if(data.value.length==0){
					$("#loadingText").hide();
					$("#norecord").show();
					$("#hasrecord").hide();
					iscroll();
				}else{
					$("#loadingText").hide();
					$("#norecord").hide();
					$("#hasrecord").show();
					analysisData(data,"#ullist",yearMonth);
				}
			}else{
				$("#loadingText").hide();
				iscroll();
				var tourl = "/mobile/mywork/qjwork.html?yearMonth="+yearMonth;
				checkLogin(data.resultCode,tourl,data.resultMessage);
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			$("#loadingText").hide();
			iscroll();
			$("#ullist").empty();
			//errorMask("网络异常");
			setPromptForInput("网络异常");
		}
	});
}
function analysisData(data,id,yearMonth){
	$(id).html('');
	var obj = data.value;
	val = obj;
	for(var i=0;i<obj.length;i++){
		var descr = obj[i].descr;
		var duration_abs = obj[i].duration_abs;
		var empl_rcd = obj[i].empl_rcd;
		var endDate = obj[i].endDate;
		var pin_take_num = obj[i].pin_take_num;
		var startDate = obj[i].startDate;
		var status = obj[i].status;
		var unit_type = obj[i].unit_type;
		var abs_entry_src = obj[i].abs_entry_src;
		var bym = startDate.substr(0,7);
		var bda = startDate.substr(8,2);
		var strstartDate = startDate.substr(0,10).replace(/-/g,'/');
		var strendDate="";
		var strEnd="";
		if(endDate!=""){
			strendDate = endDate.substr(0,10).replace(/-/g,'/');
			endDate = "至"+endDate.substr(0,10);
			strEnd="<div class=\"fn-right\">状态：<span class=\"fn-c-state\">"+status+"</span></div>";
		}else{
			endDate = "";
			strEnd="<p class=\"tar\">状态：<span class=\"fn-c-state\">"+status+"</span></p>";
		}
		var stype="小时";
		if(unit_type=="D"){
			stype="天";
		}
		if(duration_abs=="0"){
			duration_abs="";
			stype="";
		}else if(duration_abs.substr(0,1)==".")
			duration_abs="0"+duration_abs;
		var strstatus = "";
		if(status=="已提交"){
			strstatus="css-dispose";
		}else if(status=="已批准"){
			strstatus="css-disposed";
		}else if(status=="已拒绝"){
			strstatus="css-return";
		}else{
			strstatus="css-dispose";
		}
		descr = descr.substr(2,descr.length);
		var li=$("<li id=\"ckxq\" class=\""+strstatus+"\">"   
			+"<div class=\"detail-record-list-cont\">"
			+"<span class=\"css-icon\"></span>"
			+"<div class=\"list-cont\">"
			+"<div class=\"detail-record-time\">"
			+"<p class=\"time-dd\">"+bda+"</p>"
			+"<p class=\"time-yymm\">"+bym+"</p>"
			+"</div>"
			+"<div class=\"detail-record-cont\">"
			+"<h3>"+descr+duration_abs+stype+"</h3>"
			+"<div class=\"detail-record-cause\">"
			+strEnd
			+endDate
			+"</div>"
			+"</div>"
			+"</div>"
			+"</div>"
			+"<figure class=\"smallicon-sprite state-icon\"></figure>"
			+"</li>");
		$(id).append(li);
	}
	iscroll();
}

function show(){
	$('#bdate').scroller('show');
	$(".dwo").css("background-color","#000");
    $(".dwo").css("opacity",".5");
    $(".dwo").css("z-index","1000");
}
$(document).on("tap", function(){
    $(".dw dwbg").on("tap",function(){
    	return;
    })
    $(".dwo").on("tap",function(){
    	$("#promptBox").remove("slow");	
    })
    $(".dwbw dwb-c").on("tap",function(){
    	$("#promptBox").remove("slow");	
    })
    $(".dwbw dwb-s").on("tap",function(){
    	$("#promptBox").remove("slow");	
    })
});
function change(){
	var da =  $("#bdate").val();
	var yy = da.substring(0,4);
	var mm = da.substring(4,da.length);
	var yearMonth =yy+"/"+mm;
	var te = yy + ' 年    <span class="m-118" >  '+mm+'   月 </span>';
	$("#myH2").text('');
	$("#myH2").html(te);
	ycquery(yearMonth);
}
function iscroll(){
	var wh = $(window).height()-160; 
	if(myiscroll instanceof iScroll)
		myiscroll.destroy();
	$("#iscrollContent").css("height",wh+"px");
	myiscroll = new iScroll("iscrollContent",{bounce:false});
}